package com.ambow.servlet;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ambow.entity.User;

@WebServlet("/Main")
@SuppressWarnings("serial")
public class Main extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public Main() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		// response.setContentType("text/html;charset=utf-8");
		// 此出注解是因为使用CodeFilter类过滤所有Servlet，转换编码
		// request.setCharacterEncoding("utf-8");
		User user = (User) request.getSession().getAttribute("login");
		if (user == null) {
			response.sendRedirect("index.jsp");
		} else {
			request.getSession().setAttribute("nameSession", user.getAccount()); // 将用户名存入session中
			ServletContext application = this.getServletContext();

			boolean login_test = true;
			if (login_test) {
				ArrayList alist = (ArrayList) application.getAttribute("list");

				if (alist == null) {
					alist = new ArrayList();
				}
				alist.add(user.getAccount());
				application.setAttribute("list", alist);

				/*
				 * if(alist!=null&&((java.util.List) alist).size()!=0) {
				 * 
				 * }else { list.add(user.getAccount()); application.setAttribute("list", list);
				 * }
				 */
				request.getRequestDispatcher("pages/welcome.jsp").forward(request, response);
			} else {
				response.sendRedirect("pages/loginFail.jsp");
			}
		}

	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
